import numpy as np

cell_data = np.loadtxt('./data.csv', delimiter=',', encoding='utf-8')
data_count = cell_data.shape[0]
cell_per_group = 4
data_per_cell = 5
cell_count = cell_data.shape[1] // data_per_cell
group_count = cell_count // cell_per_group

def get_cell_data(cell_num):
    if cell_num > cell_count:
        return []
    data = cell_data[:, cell_num * data_per_cell: cell_num * data_per_cell + data_per_cell]
    def func(c):
        return {
            'time': c[0],
            'i': c[1],
            'v': c[2],
            'soc': c[3],
            't': c[4]
        }
    data_j = list(map(func, data))
    return data_j

def get_group_data(g_num):
    if g_num > group_count:
        return []
    time = cell_data[:, 0]
    cell_range = range(g_num * cell_per_group, g_num * cell_per_group + cell_per_group)
    vdata = []
    socdata = []
    tdata = []
    for c in cell_range:
        vdata.append(cell_data[:, c * data_per_cell + 2])
        socdata.append(cell_data[:, c * data_per_cell + 3])
        tdata.append(cell_data[:, c * data_per_cell + 4])
    # vdata = cell_data[:, g_num * cell_per_group * data_per_cell]
    vdata = np.stack(vdata, axis=0)
    socdata = np.stack(socdata, axis=0)
    tdata = np.stack(tdata, axis=0)
    vrange = np.max(vdata, axis=0) - np.min(vdata, axis=0)
    vstd = np.std(vdata, axis=0)
    socrange =np.max(socdata, axis=0) - np.min(socdata, axis=0)
    trange =np.max(tdata, axis=0) - np.min(tdata, axis=0)
    data = np.stack([time.reshape(-1), vrange.reshape(-1), vstd.reshape(-1), socrange.reshape(-1), trange.reshape(-1)], axis=1)
    # print(data.shape)
    def func(c):
        return {
            'time': c[0],
            'vrange': c[1],
            'vstd': c[2],
            'socrange': c[3],
            'trange': c[4]
        }
    data_j = list(map(func, data))
    return data_j
# print(get_cell_data(0)[0])
    
